package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysDictItem;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysDictItemService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/dict/item")
public class SysDictItemController {

    @Resource(name = "sysDictItemService")
    private SysDictItemService sysDictItemService;

    @GetMapping
    public Result<Page<SysDictItem>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, @RequestParam(value = "sysDictId") String sysDictId, String label, Integer status) throws IframeException {
        Page<SysDictItem> page = Page.of(pageNo , pageNum);
        LambdaQueryWrapper<SysDictItem> lambdaQueryWrapper = new LambdaQueryWrapper<SysDictItem>();
        if (StringUtils.isNotEmpty(sysDictId)) {
            lambdaQueryWrapper.eq(SysDictItem::getSysDictId , sysDictId);
        }
        if (StringUtils.isNotEmpty(label)) {
            lambdaQueryWrapper.like(SysDictItem::getLabel, label);
        }
        if (status != null) {
            lambdaQueryWrapper.eq(SysDictItem::getStatus , status);
        }
        lambdaQueryWrapper.orderByAsc(SysDictItem::getSort);
        return Result.ok(sysDictItemService.page(page ,lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<SysDictItem> get(@PathVariable("id") Integer id) throws IframeException{
        return Result.ok(sysDictItemService.getById(id));
    }
    @GetMapping("/list")
    public Result<List<SysDictItem>> getList(@RequestParam("code") String code) throws IframeException{
        return Result.ok(sysDictItemService.selectListByDictCode(code));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysDictItem sysDictItem) throws IframeException {
        return Result.ok(sysDictItemService.save(sysDictItem));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysDictItem sysDictItem) throws IframeException{
        return Result.ok(sysDictItemService.update(sysDictItem));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException{
        return Result.ok(sysDictItemService.delete(id));
    }

    @GetMapping("/status")
    public Result<Boolean> status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) throws IframeException {
        SysDictItem sysDictItem = new SysDictItem();
        sysDictItem.setId(id);
        sysDictItem.setStatus(status);
        return Result.ok(sysDictItemService.updateById(sysDictItem));
    }
}
